package com.example.mobifin_busticket.views.activities;

import java.util.ArrayList;
import java.util.List;

import com.example.mobifin_busticket.R;
import com.example.mobifin_busticket.R.id;
import com.example.mobifin_busticket.R.layout;
import com.example.mobifin_busticket.R.menu;
import com.example.mobifin_busticket.Util;
import com.example.mobifin_busticket.controller.databasemanager.DatabaseManager;
import com.example.mobifin_busticket.models.Boarding_DropingPointModel;
import com.example.mobifin_busticket.views.adapter.CustomListAdapterForBoardingPoint;

import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class BoardingPointInfoActivity extends Activity implements
		OnClickListener {
	CustomListAdapterForBoardingPoint boardingAdapter;
	ListView boardingList;
	TextView tvSearch;
	Button btn_cncl, btn_done;
	String SelectedValue;
	ArrayList<Boarding_DropingPointModel> bList;
	CheckBox chkbox;
	String busId, t;
	LinearLayout llError, llChildError;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_boarding_point_info);
		boardingList = (ListView) findViewById(R.id.boarding_lst_details);
		tvSearch = (TextView) findViewById(R.id.tv_search);
		llError = (LinearLayout) findViewById(R.id.ll_error);
		llChildError = (LinearLayout) findViewById(R.id.ll_child_error);
		btn_cncl = (Button) findViewById(R.id.btn_cncl);
		btn_done = (Button) findViewById(R.id.btn_Bdone);
		llError.setOnClickListener(this);
		btn_done.setOnClickListener(this);
		btn_cncl.setOnClickListener(this);
		busId = getIntent().getStringExtra("Busid");
		SelectedValue = getIntent().getStringExtra("SelectBoarding");
		System.out.println("Bus id in b activity:" + busId);
		System.out.println("Selected value from adapter:" + SelectedValue);
		DatabaseManager dbmanager = new DatabaseManager(
				BoardingPointInfoActivity.this);
		boardingAdapter.setSelectedPoint(SelectedValue);
		bList = dbmanager.getBoardingPoint(busId);

		boardingAdapter = new CustomListAdapterForBoardingPoint(
				BoardingPointInfoActivity.this, bList, SelectedValue);
		boardingList.setAdapter(boardingAdapter);

		tvSearch.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				BoardingPointInfoActivity.this.boardingAdapter.getFilter()
						.filter(s.toString());
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
		dbmanager.close();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_Bdone:
			onClickDone();
			break;
		case R.id.btn_cncl:
			finish();
			overridePendingTransition(R.anim.hold, R.anim.push_out_to_bottom);
			break;
		case R.id.ll_error:
			llError.setVisibility(View.GONE);
		default:
			break;
		}
	}

	private void onClickDone() {
		// TODO Auto-generated method stub
		System.out.println("Seleected value on done:" + SelectedValue);
		if (SelectedValue == null) {
			Animation slide = AnimationUtils.loadAnimation(
					getApplicationContext(), R.anim.slidedown);
			int noofField = 0;
			String errorMessage = "";
			llError.startAnimation(slide);
			llError.setVisibility(View.VISIBLE);
			noofField = 2;
			errorMessage = getResources().getString(
					R.string.bus_seerchbus_forget_title)
					+ ":"
					+ getResources().getString(R.string.bus_select_ur_bpoint);
			Util.errorFieldcreationSeatselect(this, llChildError, noofField,
					errorMessage);
		}
	}
}
